package com.briup.es.config.web;

import com.briup.es.web.interceptor.AuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * 配置SpringMVC相关的内容
 */
@Configuration
public class SpringWebConfig implements WebMvcConfigurer {

    @Resource
    private AuthInterceptor authInterceptor;

    // 将登录认证的拦截器，注册到SpringMVC组件中
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        registry.addInterceptor(authInterceptor)
                // 指定哪些请求路径需要被拦截，所有路径
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login", "/user/register",
                        "/book/showBooks", "/book/searchBook",
                        "/book/findByCategory/*", "/category/list",
                        "/upload",
                        // 放开部分系统资源，如swagger，druid等监控资源
                        "/profile/**",
                        "/common/download**",
                        "/common/download/resource**",
                        "/swagger-ui.html",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/*/api-docs/**",
                        "/druid/**",
                        "/favicon.ico",
                        "/doc.html"
                );
    }

    // 配置跨域请求
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 所有的路径都支持跨域
        registry.addMapping("/**")
                // 允许来自所有地方的请求
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }
}

